<style>
    .layui-form-label {
        float: left;
        display: block;
        padding: 9px 15px;
        width: 85px;
        font-weight: 400;
        line-height: 20px;
        text-align: right;
    }

    .layui-input, .layui-textarea {
        display: block;
        width: 98%;
        padding-left: 10px;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="returnGoodsTypeBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加换货类型
                        </button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="returnGoodsTypeTable" lay-filter="returnGoodsTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="returnGoodsTypeTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    {{# if(d.isDeleted===false) {  }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="enable">启动</a>
    {{# } }}
    {{# if(d.isDeleted===true) {  }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable">停用</a>
    {{# } }}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="returnTypeForm">
    <form lay-filter="returnTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">退货类型名称：</label>
            <div class="layui-input-block">
                <input name="name" placeholder="支持汉字、数字、字母" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货类型代码：</label>
            <div class="layui-input-block">
                <input name="code" placeholder="描述文字不超过128个字符" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述：</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入描述" type="text" class="layui-input" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用：</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false"
                       lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序：</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="数字，数字越小，排序越靠前" type="text" class="layui-input"
                       lay-verify="intNum" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="returnGoodsTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;

        //整数校验
        form.verify({
            intNum: [
                /^-?[0-9]\d*$/
                , '请输入整数'
            ]
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#returnGoodsTypeTable',
            url: config.base + 'enterprise/purchase/admin/returnGoodsType/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                , {field: 'name', align: 'center', title: '退货类型名称'}
                , {field: 'code', align: 'center', title: '编码'}
                , {field: 'remark', align: 'center', title: '描述'}
                , {
                    field: 'isDeleted', align: 'center', title: '是否启用', templet: function (d) {
                        if (d.isDeleted === false) {
                            return '未启用';
                        } else {
                            return '启用';
                        }
                    }
                }
                , {field: 'creatorName', align: 'center', title: '创建人'}
                , {field: 'createTime', align: 'center', title: '创建时间'}
                , {align: 'center', toolbar: '#returnGoodsTypeTableBar', title: '操作', width: 200}
            ]],
        });

        // 工具条点击事件
        table.on('tool(returnGoodsTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'enable') { // 删除
                doEnableOrDisable(data, "1");
            } else if (obj.event === 'disable') {
                doEnableOrDisable(data, "2");
            }
        });

        //监听排序
        table.on('sort(returnGoodsTypeTable)', function (obj) {
            table.reload('returnGoodsTypeTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#returnGoodsTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '660px',
                offset: '65px',
                title: data ? '修改退货类型' : '添加退货类型',
                content: $('#returnTypeForm').html(),
                success: function () {
                    var departmentName, stationsName;
                    form.val('returnTypeForm', data);
                    // 表单提交事件
                    form.on('submit(returnGoodsTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if (!isDeleted) {
                            d.field.isDeleted = false;
                        } else {
                            d.field.isDeleted = true;
                        }
                        admin.req(data ? 'enterprise/purchase/admin/returnGoodsType/update' : 'enterprise/purchase/admin/returnGoodsType/add', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('returnGoodsTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }

        function doEnableOrDisable(data, status) {
            layer.load(2);
            admin.req('enterprise/purchase/admin/returnGoodsType/disableOrEnable/' + data.id + "/" + status, {}, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'PUT');
            insTb.reload();
        }


        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].corporateName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        // 导出excel
        $('#purchaseTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('returnGoodsTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>